;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname |hw 1 adam james 1-7-09|) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp") (lib "testing.ss" "teachpack" "htdp") (lib "world.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "image.ss" "teachpack" "htdp") (lib "testing.ss" "teachpack" "htdp") (lib "world.ss" "teachpack" "htdp")))))
#|
Homework One : CSU213
Adam Blackwell
James Morin
January 7th 2009
|#

#|
Accumulator-Style Program Design
Portfolio Problems
For each of the following problems work out the solution in four different ways
 using the design recipe
 modifying the previous solution by using an accumulator
 implementing the solution using the Scheme loop foldl
 implementing the solution using the Scheme loop foldr

Problems:
1. Problem 31.3.4 in HtDP

Exercise 31.3.5.   
Develop an accumulator-style version of add-to-pi, the function that adds
a natural number to pi without using + (see section 11.5). 
Show the stage that explains what the accumulator represents.

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Using the design recipe:


;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Modifying the previous solution by using an accumulator:

|#
;; add-to-pi : number  ->  number
;; to compute the sum of the number and pi.
(define (add-to-pi num)
    (local (;; accumulator ... 
            (define (add-to-pi-accum num accum)
              (cond
                [(zero? num) accum]
                [else (add-to-pi-accum (sub1 num) (add1 accum))])))
      (add-to-pi-accum num pi)))

(check-within (add-to-pi 3) 6.14 6.15)
#|

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Implementing the solution using the Scheme loop foldl:

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Implementing the solution using the Scheme loop foldr:






;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
2. Produce a list of all authors of the books in the given list. (Use the
book definitions used in the lecture and the lab.)
|#

;; Stuctures and Examples:
(define-struct book (title author kind price))


(define a (make-book "eos" "ebw" 'n 20))
(define b (make-book "htdc" "mathias fellisian" 't 40))
(define c (make-book "the life of pi" "xyz" 'f 24))

(define lob1 (list a c c))
(define lob2 (list a b c))
(define lob3 (list a a c))

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Using the design recipe:

;; give all the authors of the list of books.
;;[Listof book] -> [Listof author]
(define (authors.v1 lob)
  (cond [(empty? lob) '()]
        [else (cons (book-author (first lob)) (authors (rest lob)))]))

(check-expect (authors.v1 lob1) (list "ebw" "xyz" "xyz"))
(check-expect (authors.v1 lob2) (list "ebw" "mathias fellisian" "xyz"))
(check-expect (authors.v1 lob3) (list "ebw" "ebw" "xyz"))

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Modifying the previous solution by using an accumulator:

;; give all the authors of the list of books.
;;[Listof book] -> [Listof author]
(define (authors lob)
  (local [;; The invarient is that the accumulator represents
          ;; the list of all the authors that we've seen so far.
          ;; authors-acum : [Listof books] [ListOf author] -> [ListOf author]
          (define (authors-acum lob a )
            (cond
              [(empty? lob) (reverse a)]
              [else (authors-acum  (rest lob) 
                                   (cons (book-author (first lob)) a))]))]
          (authors-acum lob '())))

(check-expect (authors lob1) (list "ebw" "xyz" "xyz"))
(check-expect (authors lob2) (list "ebw" "mathias fellisian" "xyz"))
(check-expect (authors lob3) (list "ebw" "ebw" "xyz"))

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Implementing the solution using the Scheme loop foldl:

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Implementing the solution using the Scheme loop foldr:

#|
-------------------------------------------------------------------------------
------------------------------------------------------------------------------- 
Pair Programming Assignment
1.1 Problem

A. A database of information about localities in the USA gives us the zip
code, the name of the locality, the state in which it is located, and the
latitude and longitude for this locality. Design the data to represent a
city in the USA that contains this information.
|#

(

#|
Note 1: We may have several different entries for a given city, for
example Boston would include the zip code 02115 and 02116, though
they would have a slightly different latitude and longitude.
Note 2: Define the location of each locality as a separate data item.

B. We would like to draw the cities on a map of the USA. Suppose our
Canvas is 100 pixels wide and 100 pixels tall. We want to convert the
latitude and longitude representation of the location into a posn on
this map. Define the function to-posn that produces a posn that
represents the given location on our Canvas.
Assume that the latitude and longitude lines are parallel to the Canvas
boundaries (parallel projection).   ... YAY ... 

Assume that
 the left edge of the Canvas is the 100 degrees of longitude (in the
Western hemisphere)
 the right edge is 60 degrees of longitude (in the Western hemisphere)

----- 100 - 60 (40) 1 degree long is 2.5 pixels, approximately 55 miles

 the top of the Canvas is 50 degrees of latitude (in the Northern
hemisphere)
 the bottom is 20 degrees of latitude (in the Northern hemisphere)

----- 20  - 50 (30) one degree lat is 3.33 pixels, approximately 70 miles

Though it may not be politically correct in this election year, we focus
only on the contiguous continental states (omitting the beautiful
states of Alaska and Hawaii).

C. Define the function distance that computes the distance (in miles)
between two cities. Estimates tell us that one degree of longitude (at
our latitude) is approximately 55 miles and one degree of latitude is
approximately 70 miles. (Feel free to make a more accurate estimates.
If you do so, include a comment explaining how you arrived at your
estimates.)

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Using the design recipe:

;; Computes the distance (in miles) between two cities:
;; distance : 

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Modifying the previous solution by using an accumulator:

D. Define the function total-distance that computes the length of a
trip on which we visit all cities in a list of cities in the order in which
they appear in the list.
Note: Design this function in several stages: the first version should
be done by following the Design Recipe. You should then modify the
function to use the accumulator style. (Hand in only the accumulator
style function.)

;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Using the design recipe:


;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Modifying the previous solution by using an accumulator:

E. Define the function all-states that produces a list of all states we
visit when our trip takes us to all cities in a given list of cities. Make
sure each state appears in the list only once.
Note: Design this function in several stages: the first version should
be done by following the Design Recipe with the necessary auxiliary
functions. You should then optimize the function so it traverses the
list only once.
;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Using the design recipe:


;;-----------------------------------------------------------------------------
;;----------------------------------------------------------------------------- 
;; Modifying the previous to optimize it:

Note2: You will get a substantial partial credit if you hand in only the
functions that follow the Design Recipe.
Note: Use the check-expect format of the testing.ss teachpack to
define all your test cases.

-------------------------------------------------------------------------------
------------------------------------------------------------------------------- 
1.2 Problem
Creative Project:

During the next four weeks you will design and program an interactive
game in the style you have done in the first course. A game consist of
several different objects. The object move either on each tick of the clock,
or in response to the keys (typically the arrow keys). There may be other
changes in the game object over the time or in response to the key events (x
key launches a shot, an animal gets hungrier as the time goes on, ...). The
objects interact in some predefined manner. Finally, something (the state of
an object, the interaction between objects) triggers the end of the game.


A. Brainstorm with your partner about a game you would like to design.
It should have at least two interacting objects, one of them should
respond to the key events, one or both of them should respond to the
time ticks. There should be an opportunity to expand the game to
multiple objects (of at least one kind).


B. Write a design proposal for your game that includes the following:
(a) Game name and a sketch of one or more game scenes.
(b) A list of objects that comprise the game. For each object describe
its attributes (what is the relevant information you will have to
keep track of).
(c) For each object (or a pair of interacting objects) describe (in English)
its behavior in response to the tick event and to the key
event.
(d) Describe (in English) what are the objects, their properties and
their positions at the start of the game.
(e) Describe (in English) what conditions will lead to the end of the
game.


C. Add a short document outlining the possible extension to your game
(how it may include multiple objects, increase the difficulty of the
game. etc.)

Note 1: The complete description should not exceed two pages. It must
be typed, though the screenshots can be drawn by hand.

Note 2: You will get comments on your design describing what features
are to be implemented, which should be postponed or modified.
|#


#|
36105, Montgomery, AL, 86.310449, 32.32573
85009, Phoenix, AZ, 112.128368, 33.456373
72201, Little Rock, AR, 92.281939, 34.748342
95819, Sacramento, CA, 121.436634, 38.568293
80204, Denver, CO, 105.025854, 39.734022
06120, Hartford, CT, 72.675807, 41.78596
19901, Dover, DE, 75.535983, 39.156639
32304, Tallahassee, FL, 84.321132, 30.447752
30306, Atlanta, GA, 84.351418, 33.786027
83705, Boise, ID, 116.219104, 43.585077
62701, Springfield, IL, 89.649531, 39.80004
46219, Indianapolis, IN, 86.049533, 39.782092
50314, Des Moines, IA, 93.632993, 41.603003
66610, Topeka, KS, 95.746061, 38.982213
40212, Louisville, KY, 85.804479, 38.265116
70815, Baton Rouge, LA, 91.059558, 30.455809
04330, Augusta, ME, 69.766548, 44.323228
21403, Annapolis, MD, 76.49103, 38.952394
02115, Boston, MA, 71.092215, 42.342706
48823, East Lansing, MI, 84.476409, 42.738805
55103, Saint Paul, MN, 93.121594, 44.960798
39209, Jackson, MS, 90.244626, 32.318422
65101, Jefferson City, MO, 92.152462, 38.546212
59635, East Helena, MT, 111.905089, 46.597324
68503, Lincoln, NE, 96.676623, 40.823339
89704, Carson City, NV, 119.828624, 39.089756
03301, Concord, NH, 71.527734, 43.218525
08618, Trenton, NJ, 74.782062, 40.237687
87505, Santa Fe, NM, 105.981994, 35.619623
12207, Albany, NY, 73.752327, 42.658133
27606, Raleigh, NC, 78.711189, 35.764499
58501, Bismarck, ND, 100.774755, 46.823448
43210, Columbus, OH, 83.016404, 40.002804
73105, Oklahoma City, OK, 97.500291, 35.510811
97301, Salem, OR, 122.979692, 44.926039
17101, Harrisburg, PA, 76.883079, 40.261767
02908, Providence, RI, 71.437684, 41.838294
29209, Columbia, SC, 80.935525, 33.965863
57501, Pierre, SD, 100.321057, 44.369514
37914, Knoxville, TN, 83.849624, 35.991755
78721, Austin, TX, 97.686798, 30.272144
84103, Salt Lake City, UT, 111.874891, 40.777584
05602, Montpelier, VT, 72.576992, 44.264082
23222, Richmond, VA, 77.426725, 37.574802
98501, Olympia, WA, 122.876311, 47.012906
25301, Charleston, WV, 81.630606, 38.349
53714, Madison, WI, 89.311758, 43.097735
82001, Cheyenne, WY, 104.796234, 41.143719
|#